package drds.binlog.database_driver.packets.server;

import drds.binlog.common.Authors;
import drds.binlog.database_driver.packets.PacketWithHeaderPacket;
import drds.binlog.database_driver.utils.Bytes;
import drds.binlog.database_driver.utils.LengthCodedStringReader;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;

@Author(name = Authors.LI_YANG)
public class FieldPacket extends PacketWithHeaderPacket
{

    @Setter
    @Getter
    private String catalogName;
    @Setter
    @Getter
    private String dataBaseName;
    //
    @Setter
    @Getter
    private String tableName;
    @Setter
    @Getter
    private String originalTableName;
    //
    @Setter
    @Getter
    private String columnName;
    @Setter
    @Getter
    private String originalColumnName;
    //
    @Setter
    @Getter
    private int character;
    @Setter
    @Getter
    private long length;
    @Setter
    @Getter
    private byte type;
    @Setter
    @Getter
    private int flags;
    @Setter
    @Getter
    private byte decimals;
    @Setter
    @Getter
    private String definition;

    /**
     * <pre>
     *  VERSION 4.1
     *  Bytes                      Name
     *  -----                      ----
     *  n (Length Coded String)    catalogName
     *  n (Length Coded String)    dataBaseName
     *  n (Length Coded String)    tableName
     *  n (Length Coded String)    org_table
     *  n (Length Coded String)    columnName
     *  n (Length Coded String)    org_name
     *
     *
     *  1                          (filler)
     *  2                          charsetnr
     *  4                          length
     *  1                          type
     *  2                          flags
     *  1                          decimals
     *  2                          (filler), always 0x00
     *  n (Length Coded Binary)    default
     *
     * </pre>
     */
    public void fromBytes(byte[] bytes) throws IOException
    {

        int index = 0;
        LengthCodedStringReader lengthCodedStringReader = new LengthCodedStringReader(index, null);
        // 1.
        this.catalogName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.dataBaseName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.tableName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.originalTableName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.columnName = lengthCodedStringReader.readLengthCodedString(bytes);
        this.originalColumnName = lengthCodedStringReader.readLengthCodedString(bytes);
        //
        index = lengthCodedStringReader.getStartIndex();//从里面获取对应的index
        //
        index++;//(filler)
        //
        this.character = Bytes.readUnsigned2IntLittleEndian(bytes, index);
        index += 2;
        //
        this.length = Bytes.readUnsigned4IntLittleEndian(bytes, index);
        index += 4;
        //
        this.type = bytes[index];
        index++;
        //
        this.flags = Bytes.readUnsigned2IntLittleEndian(bytes, index);
        index += 2;
        //
        this.decimals = bytes[index];
        index++;
        //
        index += 2;// skip event_filter
        //
        if (index < bytes.length)
        {
            lengthCodedStringReader.setStartIndex(index);//new start log_position_manager
            this.definition = lengthCodedStringReader.readLengthCodedString(bytes);
        }
    }

    public byte[] toBytes() throws IOException
    {
        return null;
    }


}
